package com.jungle.client;

import com.jungle.bfi.domain.vo.DataCategoryVo;
import com.jungle.client.control.BasicDialog;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URL;
import java.util.function.Consumer;

import static com.jungle.client.FxApplication.bundle;

@Slf4j
public class DataCategoryDialogController extends BasicDialog<DataCategoryVo> {

    private String id;
    @FXML
    private TextField nameField;

    @FXML
    private TextField codeField;

    @Override
    public void loadData(DataCategoryVo data) {
        log.info("load data:{}", data);
        this.id = data.getId();
        this.nameField.setText(data.getName());
        this.codeField.setText(data.getCode());
    }

    @Override
    public DataCategoryVo storeData() {
        DataCategoryVo data = new DataCategoryVo();
        data.setId(id);
        data.setName(nameField.getText());
        data.setCode(codeField.getText());
        return data;
    }

    private static final URL FXML_URL = DataCategoryDialogController.class.getResource("dataCategory-edit.fxml");
    private static final String TITLE = bundle.getString("dataCategory.dialog.title");
    private static final String ADD_TITLE = bundle.getString("dialog.title.prefix.add") + TITLE;
    private static final String EDIT_TITLE = bundle.getString("dialog.title.prefix.edit") + TITLE;

    public static void showAddDialog(DataCategoryVo dataSetVo, Consumer<DataCategoryVo> consumer) throws IOException {
        new DataCategoryDialogController().showDialog(FXML_URL, ADD_TITLE, dataSetVo, consumer);
    }


    public static void showEditDialog(DataCategoryVo dataSetVo, Consumer<DataCategoryVo> consumer) throws IOException {
        new DataCategoryDialogController().showDialog(FXML_URL, EDIT_TITLE, dataSetVo, consumer);
    }
}
